package client

import (
	"context"
	"net/http"
	"wms/model"

	"wms/internal/svc"
	"wms/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type ClientUpdateLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewClientUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ClientUpdateLogic {
	return &ClientUpdateLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ClientUpdateLogic) ClientUpdate(req *types.ClientUpdateRequest) (resp *types.BaseResponse, err error) {
	resp = new(types.BaseResponse)
	//1.客户名称是否存在
	_, err = l.svcCtx.ClientModel.FindOne(l.ctx, req.Id)
	switch err {
	case nil: //不处理
	case model.ErrNotFound: //不存在
		resp.Code = http.StatusBadRequest
		resp.Msg = "客户不存在"
		return resp, nil
	default: //其他错误
		resp.Code = http.StatusInternalServerError
		resp.Msg = "服务内部错误"
		return resp, nil
	}

	//2.客户信息入库
	err = l.svcCtx.ClientModel.Update(l.ctx, &model.Client{
		Id:   req.Id,
		Name: req.Name,
	})

	if err != nil {
		if err == model.ErrNotFound {
			resp.Code = http.StatusNoContent
			resp.Msg = "客户不存在"
			return resp, nil
		}

		resp.Code = http.StatusInternalServerError
		resp.Msg = "服务内部错误"
		return resp, nil
	}

	resp.Code = http.StatusOK
	resp.Msg = "成功"
	return resp, nil
}
